/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.util.ArrayList;
import java.util.UUID;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewCustomAdapter extends BaseAdapter {

	private ArrayList<Character> characterList;

	public Activity context;
	public LayoutInflater inflater;
	public UUID uniqueId;

	public ListViewCustomAdapter(Activity context, ArrayList<Character> itemList) {
		super();

		this.context = context;
		this.characterList = itemList;

		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * @return the characterList
	 */
	public ArrayList<Character> getCharacterList() {
		return characterList;
	}

	/**
	 * @param characterList
	 *            the characterList to set
	 */
	public void setCharacterList(ArrayList<Character> characterList) {
		this.characterList = characterList;
	}

	@Override
	public int getCount() {
		return characterList.size();
	}

	@Override
	public Object getItem(int position) {
		return characterList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public static class ViewHolder {
		TextView txtCharName;
		TextView txtLevel;
		TextView txtRace;
		TextView txtClass;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.maincharselect, null);

			holder.txtCharName = (TextView) convertView.findViewById(R.id.txtSelectCharName);
			holder.txtLevel = (TextView) convertView.findViewById(R.id.txtSelectCharLevel);
			holder.txtRace = (TextView) convertView.findViewById(R.id.txtSelectRace);
			holder.txtClass = (TextView) convertView.findViewById(R.id.txtSelectClass);

			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		Character character = (Character) characterList.get(position);

		holder.txtCharName.setText(character.info.getName());
		holder.txtLevel.setText("Level " + String.valueOf(character.characterClass.getLevel()));
		holder.txtRace.setText(character.info.getRace());
		holder.txtClass.setText(character.characterClass.getClassDescriptionString());

		return convertView;
	}

	public void removeCharacter(UUID id) {
		uniqueId = id;
		for (Character ch : characterList) {
			if (uniqueId == ch.getUniqueId()) {
				characterList.remove(ch);
				Log.d("CHAR", ch.info.getName() + " was deleted");
				break;
			}
		}
	}
}